package main

import (
	"fmt"
	"strconv"
)

const Usage = `
Usage:
	c ADDRESS "创建区块链"
	p "打印区块链"
	px "打印交易"
	bb ADDRESS "获取指定地址余额（账本）"
	bm ADDRESS "获取指定地址余额（内存）"
	s <FROM> <TO> <AMOUNT> "转账"
	m [MINER] [DATA] "挖矿"，默认:  1NVwrN4yZVV3hW1PkXCg38sGcsXMKcYaw7
	cwt "创建钱包地址"
	l "打印钱包中的所有地址"
	ss "查看当前待确认交易数量"
	r "重新加载utxo到内存"
`

//定义一个CLI，里面包含BlockChain，所有细节工作交给bc，命令的解析工作交给CLI
type CLI struct {
	//bc *BlockChain
}

func checkArgs(cmds []string, count int) bool {
	if len(cmds) != count {
		fmt.Println("参数无效!")
		//os.Exit(1)
		return false
	}

	return true
}

//定义一个run函数，负责接收命令行的数据，然后根据命令进行解析，并完成最终的调用
func (cli *CLI) Run(cmds []string) {
	//args := os.Args
	args := cmds

	//if len(args) < 2 {
	//	fmt.Println(Usage)
	//	//os.Exit(1)
	//	return
	//}

	cmd := args[0]

	switch cmd {

	case "c":
		fmt.Printf("创建区块链命令被调用!\n")
		//if !checkArgs(cmds, 2) {
		//	return
		//}
		//address := args[1]
		address := "1NVwrN4yZVV3hW1PkXCg38sGcsXMKcYaw7"
		cli.CreateBlockChain(address)

	case "p":
		fmt.Printf("打印区块命令被调用\n")
		if !checkArgs(cmds, 1) {
			return
		}
		cli.PrintChain()

	case "px":
		fmt.Printf("打印交易命令被调用\n")
		if !checkArgs(cmds, 1) {
			return
		}
		cli.PrintTx()

	case "bb":
		fmt.Printf("bb获取余额命令被调用\n")
		if !checkArgs(cmds, 2) {
			return
		}
		address := args[1]
		cli.GetBalanceDb(address)
	case "bm":
		fmt.Printf("bm获取余额命令被调用\n")
		if !checkArgs(cmds, 2) {
			return
		}
		address := args[1]
		cli.GetBalanceMem(address)

	case "s":
		fmt.Printf("转账send命令被调用\n")
		if !checkArgs(cmds, 4) {
			return
		}

		from := args[1]
		to := args[2]
		amount, _ := strconv.ParseFloat(args[3], 64) //string
		cli.Send(from, to, amount)

	case "m":
		fmt.Printf("mine命令被调用\n")
		//if !checkArgs(cmds, 4) {
		//	return
		//}

		//mine address data
		var miner string
		var data string

		if len(cmds) == 3 {
			miner = cmds[1]
			data = cmds[2]
		} else {
			data = "奖励xxxx 50.0"
			miner = "1NVwrN4yZVV3hW1PkXCg38sGcsXMKcYaw7"
		}

		cli.Mine(miner, data)

	case "cwt":
		fmt.Printf("createWallet命令被调用\n")
		if !checkArgs(cmds, 1) {
			return
		}

		cli.CreateWallet()
	case "l":
		fmt.Printf("listAllAddress命令被调用\n")
		//if !checkArgs(cmds, 1) {
		//	return
		//}
		var flag int64 = 1 //默认是1，打印余额
		if len(cmds) == 2 {
			flag = stringToInt(cmds[1])
		}
		cli.ListAllAddress(flag)
	case "ss":
		cli.Status()
	case "r":
		cli.Reload()

	default:
		fmt.Printf("无效的命令，请检查!\n")
		cli.Help()
	}
}
